package com.huaxin.common.media;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;

import com.huaxin.common.BaseFragment;
import com.huaxin.common.R;
import com.huaxin.common.cache.DiskCache;
import com.huaxin.common.media.contract.SelectImageContract;
import com.huaxin.common.media.view.ClipImageBorderView;
import com.huaxin.common.media.view.ClipZoomImageView;
import com.huaxin.common.utils.ToastUtil;

/**
 * 裁剪图片的Activity
 *
 * @ClassName: CropImageActivity
 * @Description:
 * @author xiechengfa2000@163.com
 * @date 2015-5-8 下午3:39:22
 */
public class ClipFragment extends BaseFragment implements OnClickListener {


    private String _path;
    private ClipZoomImageView mZoomView;
    private ClipImageBorderView mBorderView;
    private int mHorizontalPadding = 50;
    private SelectImageContract.Operator mOperator;

    @Override
    public void onAttach(Context context) {
        this.mOperator = (SelectImageContract.Operator) context;
        super.onAttach(context);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_crop_image;
    }


    @Override
    public void initView(View view) {
        mZoomView = (ClipZoomImageView) view.findViewById(R.id.zoom_view);
        mBorderView = (ClipImageBorderView) view.findViewById(R.id.border_view);

        view.findViewById(R.id.okBtn).setOnClickListener(this);
        view.findViewById(R.id.cancleBtn).setOnClickListener(this);


        mHorizontalPadding = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources()
                        .getDisplayMetrics());
        mBorderView.setHorizontalPadding(mHorizontalPadding);
        mZoomView.setHorizontalPadding(mHorizontalPadding);
        _path = getArguments().getString("path");
        getGlide().load(_path).into(mZoomView);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.okBtn) {
            Bitmap bitmap = mZoomView.clip();
            String fileName = DiskCache.getFileName(_path);
            if(!DiskCache.putBitmap(fileName,bitmap)){
                ToastUtil.showToast("裁剪失败");
            }

            mOperator.getCallback().doSelectDone(
                    Util.toArray(_path),Util.toArray( DiskCache.getFilePath(fileName)));
            getActivity().finish();
        } else if(v.getId() == R.id.cancleBtn){
            finish();
        }

    }

}
